<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, type FormInstance } from "element-plus";
import { getImageApi, loginApi } from "@/apis/login";
import { userStore } from "@/stores/user";
const user = userStore();
const name = "Login";
const loginForm = ref<FormInstance>();

const addModel = reactive({
  username: "",
  password: "",
  code: "",
});



function generateRandomCaptcha() {
  return Math.random().toString().substr(2, 4);
}

const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 4, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" },
  ],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
});

const submitForm = () => {
  loginForm.value?.validate(async (valid) => {
    if (valid) {
      let res = await loginApi(addModel);
      if (res.code == 200) {
        ElMessage.success("登录成功");
        user.setId(res.data.id);
        user.setNickname(res.data.nickname);
        user.setToken(res.data.token);
        setTimeout(() => {
          window.location.href = "/";
        }, 1000);
      }
    }
  });
};
const images = ref("");
const getImage = async () => {
  let res = await getImageApi();
  images.value = res.data;
};
onMounted(() => {
  getImage();
});

const resentForm = () => {
  loginForm.value?.resetFields();
};
</script>
<template>
  <div class="login-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>登录</span>
        </div>
      </template>
      <el-form
        :model="addModel"
        :rules="rules"
        ref="loginForm"
        label-width="100px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="addModel.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="addModel.password"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input
                v-model="addModel.code"
                @keyup.enter="submitForm"
                placeholder="请输入验证码"
              ></el-input>
            </el-col>
            <el-col :span="10"> <img class="image" :src="images" </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm"
            class="login-button"
            size="defalut"
            >登录</el-button
          >
          <el-button size="defalut" class="reset-button" @click="resentForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #6e8efb, #a777e3);
}

.box-card {
  width: 400px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
  font-size: 1.5em;
  color: #333;
}

.login-form {
  padding: 20px;
  background-color: #fff; /* 白色背景 */
  border-radius: 10px; /* 圆角 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  margin-top: -40px; /* 上移覆盖卡片头部 */
}
.image {
  width: 90px;
  height: 30px;
}

/* 调整表单

.el-input__inner {
  border-radius: 5px;
}

.el-button {
  width: 100%;
  border-radius: 5px;
}

/* 特定于验证码按钮的样式 */
.el-button + .el-button {
  margin-left: 0;
}
.login-button {
  margin-right: 10px;
  width: 80px;
}
.reset-button {
  width: 80px;
}
</style>
